/**
 * @author 老刘
 * @desc 下拉选择
 */
import { formatNumber } from '../../../utils/util.js';
const app = getApp();
Page({
	data: {
		isSite: false,
		choiceAllList: [],// 全部选项
		choiceOneList: [],
		choiceTwoList: [],
		chooseTime: '',
		chooseOptions: '全部选项',
		initializationDate: undefined,// 初始化日期
		initializationChoice: undefined,// 初始化选项
		year: '',
		dropDownMenuTitle: ['', ''],
		timeList: [],
		childModel: [
			{ id: '1-1', title: '1' },
			{ id: '1-2', title: '2' },
			{ id: '1-3', title: '3' },
			{ id: '1-4', title: '4' },
			{ id: '1-5', title: '5' },
			{ id: '1-6', title: '6' },
			{ id: '1-7', title: '7' },
			{ id: '1-8', title: '8' },
			{ id: '1-9', title: '9' },
			{ id: '1-10', title: '10' },
			{ id: '1-11', title: '11' },
			{ id: '1-12', title: '12' }
		],
		list: [],
		choiceList: [
			{type: 1, name: '老刘'},
			{type: 1, name: '老王'},
			{type: 2, name: '老张'},
			{type: 1, name: '老李'},
			{type: 2, name: '老郑'}
		]
	},
	onLoad (options) {
		// 计算日期
		let date = new Date();
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const time = year - 2018;
		for (let i = 0; i <= time; i = i + 1) {
			for (let k = 0; k < this.data.childModel.length; k = k + 1) {
				this.data.childModel[k].id = `${i}-${k + 1}`;
				const obj = {};
				obj.id = this.data.childModel[k].id;
				obj.title = this.data.childModel[k].title;
				const h = time - i;
				this.data.list.push(obj);
				this.setData({
					list: this.data.list,
					[`timeList[${time - i}].childModel`]: this.data.list
				});
			}
			this.setData({
				list: [],
				[`timeList[${time - i}].title`]: `${year - i}年`,
				[`timeList[${time - i}].id`]: `${i}`
			});
		}
		if (options.choice) {
			// 月账单推送进入
			let dateArr = options.month.split('-');
			this.setData({
				[`dropDownMenuTitle[0]`]: options.choice,
				[`dropDownMenuTitle[1]`]: `${dateArr[0]}年${formatNumber(dateArr[1])}月`,
				year: `${year}`,
				chooseTime: `${dateArr[0]}-${formatNumber(dateArr[1])}`,
				initializationChoice: options.choice,
				chooseOptions: options.choice,
				initializationDate: `${dateArr[0]}${formatNumber(dateArr[1])}`
			});
		} else {
			this.setData({
				[`dropDownMenuTitle[0]`]: this.data.choiceAllList[0],
				[`dropDownMenuTitle[1]`]: `${year}年${month}月`,
				year: `${year}`,
				chooseTime: `${year}-${formatNumber(month)}`
			});
		}
		this.getOptions();
	},
	getOptions () {
		let [choiceAllList, choiceOneList, choiceTwoList] = [[], [], []];
		this.data.choiceList.map(item => {
			choiceAllList.push(item.name);
			if (item.type === 1) {
				choiceOneList.push(item.name);
			} else {
				choiceTwoList.push(item.name);
			}
		});
		this.setData({
			choiceAllList,
			choiceOneList,
			choiceTwoList
		});
	},
	// 下拉选择
	selectedItem (e) {
		let choiceAllList = [];
		const vehicle = e.detail.selectedId ? this.data.chooseOptions : e.detail.selectedTitle;
		if (vehicle === '全部选项') {
			choiceAllList = this.data.choiceAllList;
		} else if (vehicle === '选项一') {
			choiceAllList = this.data.choiceOneList;
		} else {
			choiceAllList = this.data.choiceTwoList;
		}
		if (!e.detail.selectedId) {
			this.setData({
				initializationChoice: undefined
			});
			const vehicleType = ['全部选项', '选项一', '选项二'];
			if (vehicleType.includes(e.detail.selectedTitle)) { // 统计点击全部选项
				this.setData({
					chooseOptions: e.detail.selectedTitle
				});
				// 选择的是一级选项
			} else {
				this.setData({
					chooseOptions: e.detail.selectedTitle
				});
			}
		} else {
			this.setData({
				initializationDate: undefined
			});
			const month = e.detail.selectedId.match(/-(\S*)/)[1];
			const id = e.detail.selectedId.match(/(\S*)-/)[1];
			this.setData({
				chooseTime: `${this.data.year - id}-${formatNumber(month)}`
			});
		}
	}
});
